package com.example.sleepdebtcalculatorandroid;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
	private static final String DATABASE = "sleepCalculator";
	private static final int VERSION = 9;
	
	public DatabaseHelper(Context context) {
		super(context, DATABASE, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
	db.execSQL("CREATE TABLE idealSleepAmount (id INTEGER PRIMARY KEY AUTOINCREMENT, idealSleepAmountHours TEXT, idealSleepAmountMinutes TEXT);");
	db.execSQL("CREATE TABLE actualSleepAmount (id INTEGER PRIMARY KEY AUTOINCREMENT, day TEXT, month TEXT, year TEXT, actualSleepAmountHours TEXT, actualSleepAmountMinutes);");
	initializeActualSleepAmountTable(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		android.util.Log.w("Database", "Upgrading database, which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS idealSleepAmount");
		db.execSQL("DROP TABLE IF EXISTS actualSleepAmount");
		onCreate(db);
	}

	public void initializeActualSleepAmountTable(SQLiteDatabase db) {
		ContentValues contentValues = new ContentValues();
		for(int month = 0; month < 12; month++){
			for(int day = 1; day < 32; day++){
				if(month == 1 && day > 28)
					continue;
				else if( (month == 3 || month == 5 || month == 8 || month == 10) && day > 30)
					continue;
				else {
					contentValues.put("day", day);
					contentValues.put("month", month);
					contentValues.put("year", "2012");
					contentValues.put("actualSleepAmountHours", "0");
					contentValues.put("actualSleepAmountMinutes", "0");
					db.insert("actualSleepAmount", null, contentValues);
				}
			}
		}		
	}
	
	

	
}
